package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.MenuMapper;
import com.qf.dao.PermsettingMapper;
import com.qf.pojo.Menu;
import com.qf.pojo.Permsetting;
import com.qf.pojo.Usermanager;
import com.qf.service.PermsettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/20 0020
 * Author->DT
 * qmxc
 * 行到水穷处 坐看云起时
 * 模块：职位模块Service层实现
 */
@Service
public class PermsettingServiceImpl implements PermsettingService {
    @Autowired
    private PermsettingMapper mapper;
    @Autowired
    private MenuMapper menuMapper;
    //根据deptID查询全部职位信息
    @Override
    public List<Permsetting> findAllRolesByDeptId(Integer deptId) {
        List<Permsetting> roles = mapper.findAllRolesByDeptId(deptId);
        return roles;
    }

    //分页条件查询所有对应职位信息
    @Override
    public PageInfo<Permsetting> findAllRoleInfoByLimit(Map map) {
        //1.开启分页
        PageHelper.startPage((int)map.get("index"), (int)map.get("page"));
        //2.查询职位列表
        List<Permsetting> roleList=mapper.findAllRoleInfoByLimit(map);
        //遍历集合，设置条目状态：
        for (Permsetting permsetting : roleList) {
            if (permsetting.getUsed()==1){
                //表示已启用
                permsetting.setUseState("启用");
            }else {
                //表示禁用
                permsetting.setUseState("禁用");
            }
        }
        //3，封装PageInfo
        PageInfo<Permsetting> pageInfo = new PageInfo<>(roleList);
        //4.返回
        return pageInfo;
    }

    //执行添加
    @Override
    @Transactional
    public Integer add(Permsetting role, Integer[] mids) {
        //添加角色信息到职位表
        //设置addTime
        role.setAddtime(new Date());
        //执行添加，并的到自增后的主键值
        mapper.insert(role);
        //添加菜单权限信息到菜单权限表
        //封装参数
        Map map=new HashMap();
        map.put("rid",role.getId());
        map.put("mids",mids);
        Integer count=menuMapper.insert(map);
        return count;
    }

    //根据id查询角色信息
    @Override
    public Permsetting selectByPrimaryKey(Integer rid) {
        //查询角色id对应的角色信息
        Permsetting role = mapper.selectByPrimaryKey(rid);
        //查询角色菜单表中该角色对应的菜单id
        List<Integer> mids=menuMapper.selectMidsByRidOfRoleMenu(rid);
        role.setMids(mids);
        return role;
    }

    //修改职位信息
    @Override
    public int updateByPrimaryKeySelective(Permsetting record) {
        return mapper.updateByPrimaryKeySelective(record);
    }
}
